package com.develope.menu;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<Men> {
	Activity context = null;
	ArrayList<Men> myArray = null;
	int layoutId;
	
	//Khoi tao ham MyArrayAdapter
	
	public MyArrayAdapter(Activity context, int layoutId, ArrayList<Men> arr) {
		super(context, layoutId, arr);
		this.context = context;
		this.layoutId = layoutId;
		this.myArray = arr;
	}
	
	//Gan gia tri vao ListView
	
	public View getView(int position, View converView, ViewGroup parent) {
		LayoutInflater inflater = context.getLayoutInflater();
		converView = inflater.inflate(layoutId, null);
		if (myArray.size() > 0 && position >= 0) {
			final TextView txtDisplay1 = (TextView) converView
					.findViewById(R.id.txtBan);
			final Men men = myArray.get(position);
			txtDisplay1.setText(men.getBan());

			final TextView txtDisplay2 = (TextView) converView
					.findViewById(R.id.txtMon);
			txtDisplay2.setText(men.getMon());

			final TextView txtDisplay3 = (TextView) converView
					.findViewById(R.id.txtSluong);
			txtDisplay3.setText(men.getSluong().toString());

		}
		return converView;
	}

	// Chuyen doi
	public String dataToString() {
		String rs = "";
		if (myArray != null) {
			for (Men item : myArray) {
				rs += item.toString() + ";";
			}
			int len = rs.length();
			if (len > 0) {
				rs = rs.substring(0, len - 2);
			}
		}
		return rs;
	}
}
